
Component({
  options: {
    multipleSlots: true,
    addGlobalClass: true,
  },

  properties: {
    goodsData: {
      type: null,
      value: null,
      observer(goodsData) {
        this.parseData(goodsData);
      }
    },
    show: {
      type: Boolean,
      value: false,
    },
    checkSkuIdArr: {
      type: Array,
      value: []
    },
    defaultBuyNum: {
      type: Number,
      value: 1
    },
    limitMinCount: {
      type: Number,
      value: 1,
    },
    limitBuyInfo: {
      type: String,
      value: '',
    },
  },

  data: {
    title: '',
    src: '',
    specList: [],
    minSalePrice: 0,
    maxLinePrice: 0,
    limitMaxCount: 999,
    buyNum: 1,
    selectedSpecTitle: '请选择规格',
    isStock: true,
  },

  methods: {
    parseData: function (data) {
      if(!data) return;
      this.setData({
        title: data.Name,
        src: data.Pictures[0].NormalUrl,
        specList: data.Attrs,
        isStock: data.StockQuantity > 0,
        minSalePrice: data.Price,
        maxLinePrice: data.OldPrice
      })
    },

    toChooseItem({currentTarget}) {
      let checkSkuItem = currentTarget.dataset.item
      let spuIndex = currentTarget.dataset.spuindex
      let spuValueIndex = currentTarget.dataset.spuvalueindex
      let specList = this.data.specList
      let checkSkuIdArr = []
      let minSalePrice = this.data.minSalePrice
      let limitMaxCount = this.data.limitMaxCount

      if(this.properties.checkSkuIdArr.length === 0){
        checkSkuIdArr = Array(specList.length).fill(0);
      }else{
        checkSkuIdArr = this.data.checkSkuIdArr
      }
      if(checkSkuIdArr[spuIndex] > 0){
        checkSkuIdArr[spuIndex] = 0
      }
      checkSkuIdArr[spuIndex] = checkSkuItem.Id
      specList[spuIndex].Values.forEach((specValueItem, index) => {
        if(specValueItem.IsPreSelected){
          specValueItem.IsPreSelected = false
        }
        if(index === spuValueIndex){
          specValueItem.IsPreSelected = true
        }
      })
      let attributeValue = "combination"
      let selectedAttrStr = "件"
      checkSkuIdArr.forEach((ids) => {
        if(ids > 0) {
          attributeValue = attributeValue + "_" + ids
        }
      })
      specList.forEach((specItem) => {
        specItem.Values.forEach((specValueItem) => {
          if(specValueItem.IsPreSelected){
            selectedAttrStr = selectedAttrStr + " ，" +specValueItem.Name
          }
        })
      })
      if (!checkSkuIdArr.includes(0)) {
        let skuList = this.properties.goodsData.AttrValueList
        skuList.forEach((skuItem) => {
          if(skuItem.AttributeValue === attributeValue){
            minSalePrice = skuItem.Price
            limitMaxCount = skuItem.Qty
          }
        })
      }
      this.setData({
        specList,
        limitMaxCount,
        minSalePrice,
        selectedSpecTitle: "选择："
      })

      this.triggerEvent('toChooseItem', {
        checkSkuIdArr,
        selectedLabel: '已选',
        selectedAttrStr
      });
    },

    handlePopupHide() {
      this.triggerEvent('closeSpecsPopup', {
        show: false,
      });
    },

    addCart(e) {
      const { isStock } = this.properties;
      if (!isStock) return;
      this.triggerEvent('addCart');
    },

    buyNow() {
      const { isStock } = this.properties;
      if (!isStock) return;
      this.triggerEvent('buyNow');
    },

    handleBuyNumChange(e) {
      let { value } = e.detail;
      value = parseInt(value)
      if(isNaN(value)){
        value = 1
      }
      this.setData({
        buyNum: value
      })
      this.triggerEvent('buyNumChange', {
        value
      });
    }
  },
});
